<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/edit.css">
    <link rel="stylesheet" href="blog-editormd/css/editormd.css" />

</head>

<body>
    <div class="nav">
        <img src="pic/logo2.jpg" alt="">
        <span class="blog-title">我的博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
        <a class="nav-span" href="#" onclick="logout()">注销</a>
    </div>
    <div class="content-edit">
        <div class="push">
            <input type="hidden" id="blogId">
            <input type="text" name="" id="title">
            <input type="button" value="更新文章" id="submit" onclick="submit()">
        </div>
        <!-- markdown 插件 html代码 -->
        <div id="editor">
            <textarea style="display:none;" id="content">##在这里写下一篇博客</textarea>
        </div>
    </div>

    <script src="js/jquery.min.js"></script>
    <script src="blog-editormd/editormd.min.js"></script>
    <script src="js/common.js"></script>
    <script type="text/javascript">

        //更新文章,调用submit方法
        function submit() {
           $.ajax({
               url:"/blog/update",
               type:"post",
               contentType:"application/json",
               data: JSON.stringify({//转换成JSON对象
                   "id": $("#blogId").val(),
                   "title": $("#title").val(),
                   "content":$("#content").val()
               }),
               success: function(result){
                   if(result.code == 200 && result.data == true){
                       //更新成功,跳转到博客列表
                       alert("更新成功");
                       location.href = "blog_list.html";
                   }
               },
               error: function( error) {
                   if(error.status == 401){
                       location.href = "blog_login.html";
                   }
               }
           });
        }

        function getBlogInfo() {
            //通过location.search获取url参数(此时这里包含之前博客列表返回的BlogId),然后再向后端发送ajax请求获取博客的详细信息
            $.ajax({
                type:"get",
                url:"/blog/getBlogDetail" + location.search,
                success:function (result) {
                    if(result.code == 200 && result.data != null){
                        //将后端获取到的数据先存储在一个新定义的变量中
                        var blog = result.data;
                        //将博客标题、日期、内容等信息渲染到到页面中
                        $("#blogId").val(blog.id);
                        $("#title").val(blog.title);
                        editormd("editor", {
                            width  : "100%",
                            height : "550px",
                            path: "blog-editormd/lib/",
                            onload: function () {
                                this.watch();
                                this.setMarkdown(blog.content);
                            }
                        });

                        // $(".content .detail").text(blog.content);
                        //使用 editor.md 的方法将内容渲染到页面中
                    } else {
                        //其它场景的处理
                    }
                },
                //后端返回的错误信息
                error: function (error) {
                    if(error.status == 401){
                        location.href = "blog_login.html";
                    }
                }
            });
        }
        getBlogInfo();
    </script>
</body>

</html>